{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Binary Number to String LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: printBin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #二进制数转字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>二进制数转字符串。给定一个介于0和1之间的实数（如0.72），类型为double，打印它的二进制表达式。如果该数字无法精确地用32位以内的二进制表示，则打印“ERROR”。</p>\n",
    "\n",
    "<p><strong>示例1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong> 输入</strong>：0.625\n",
    "<strong> 输出</strong>：\"0.101\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong> 输入</strong>：0.1\n",
    "<strong> 输出</strong>：\"ERROR\"\n",
    "<strong> 提示</strong>：0.1无法被二进制准确表示\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>32位包括输出中的 <code>\"0.\"</code> 这两位。</li>\n",
    "\t<li>题目保证输入用例的小数位数最多只有 <code>6</code> 位</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [binary-number-to-string-lcci](https://leetcode.cn/problems/binary-number-to-string-lcci/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [binary-number-to-string-lcci](https://leetcode.cn/problems/binary-number-to-string-lcci/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        ans = \"0.\"\n",
    "        for i in range(8):\n",
    "            num = num * 2\n",
    "            if num >= 1:\n",
    "                ans += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                ans += \"0\"\n",
    "            if num == 0:\n",
    "                break\n",
    "        if num == 0:\n",
    "            return ans\n",
    "        else:\n",
    "            return \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        \n",
    "        temp = num\n",
    "        res = ''\n",
    "        while temp:\n",
    "            if len(res)>=32:\n",
    "                return \"ERROR\"\n",
    "            temp *=2\n",
    "            if temp>=1:\n",
    "                res +='1'\n",
    "                temp = temp-1\n",
    "            else:\n",
    "                res+='0'\n",
    "        res = '0.'+res\n",
    "        return res \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        digit = 1\n",
    "        factor = 0.5\n",
    "        err = 1\n",
    "        ans = \"0.\"\n",
    "        # 退出循环的精度要设置好，要尽可能地小\n",
    "        # 2^(-33) = 1.6e-10\n",
    "        while digit<=32 and num>1e-12:\n",
    "            if num >= factor:\n",
    "                num -= factor\n",
    "                ans += \"1\"\n",
    "            else: # num < factor\n",
    "                ans += \"0\"\n",
    "            digit += 1\n",
    "            factor *= 0.5\n",
    "        if digit>32:\n",
    "            return \"ERROR\"\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = [\"0.\"]\n",
    "        for _ in range(6):  # 至多循环 6 次\n",
    "            num *= 2\n",
    "            if num < 1:\n",
    "                s.append('0')\n",
    "            else:\n",
    "                s.append('1')\n",
    "                num -= 1\n",
    "                if num == 0:\n",
    "                    return ''.join(s)\n",
    "        return \"ERROR\"\n",
    "\n",
    "# 作者：灵茶山艾府\n",
    "# 链接：https://leetcode.cn/problems/binary-number-to-string-lcci/solutions/2141577/zheng-ming-zhi-duo-xun-huan-6-ci-pythonj-b6sj/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = \"0.\"\n",
    "        while len(res) < 33 and num != 0:\n",
    "            num = num * 2\n",
    "            intnum = int(num)\n",
    "            res = res + str(intnum)\n",
    "            num = num - intnum\n",
    "        if len(res) < 33:\n",
    "            return res\n",
    "        else:\n",
    "            return \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res=\"0.\"\n",
    "        while num!=0 and len(res)<=32:\n",
    "            num*=2\n",
    "            if num>=1:\n",
    "                res+=\"1\"\n",
    "                num-=1\n",
    "            else:\n",
    "                res+=\"0\"\n",
    "        return res if num==0 else \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = []\n",
    "        a = num\n",
    "        for i in range(32):\n",
    "            b = a*2\n",
    "            if b>1:\n",
    "                a = a*2 - 1\n",
    "                res.append('1')\n",
    "                continue\n",
    "            elif b == 1:\n",
    "                a = 0\n",
    "                res.append('1')\n",
    "                break\n",
    "            elif b<1:\n",
    "                a = a*2\n",
    "                res.append('0')\n",
    "                continue\n",
    "        if a == 0:\n",
    "            return (\"0.\"+\"\".join(res))\n",
    "        else:\n",
    "            return 'ERROR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        # visitied = set([num])\n",
    "        res = '0.'\n",
    "        flag = True\n",
    "        while num!=0:\n",
    "            num*=2\n",
    "            res+=str(math.floor(num))\n",
    "            if len(res)>32:return \"ERROR\"\n",
    "            if num>=1:num-=1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = \"0.\"\n",
    "        \n",
    "        while len(s) <= 32 and num != 0:\n",
    "            num *= 2\n",
    "            integer = int(num)\n",
    "            s += str(integer)\n",
    "            num -= integer\n",
    "\n",
    "        return s if len(s) <= 32 else \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        a = 0.5\n",
    "        k = 1\n",
    "        s = 0.0\n",
    "        t = '0.'\n",
    "        while s != num:\n",
    "            if num - s >= a:\n",
    "                s = s + a\n",
    "                t += '1'\n",
    "            else:\n",
    "                t += '0'\n",
    "            k = k + 1\n",
    "            a = 0.5 ** k\n",
    "            if k > 30:\n",
    "                return \"ERROR\"\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        count = 2\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                count += 1\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "                count += 1\n",
    "        if len(binary) > 32:\n",
    "            return \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        k = 32\n",
    "        if num % 2 ** (-k):\n",
    "            return 'ERROR'\n",
    "\n",
    "        s = '0.'\n",
    "        for i in range(1, k+1):\n",
    "            if num >= (2 ** -i):\n",
    "                s += '1'\n",
    "                num = num - (2 ** -i)\n",
    "            else:\n",
    "                s += '0'\n",
    "            \n",
    "            if num == 0:\n",
    "                break\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        ans='0.'\n",
    "        p=-1\n",
    "        while num<2**(p+1) and len(ans)<=32:\n",
    "            if num>2**p:\n",
    "                num-=2**p\n",
    "                ans+='1'\n",
    "            elif num==2**p:\n",
    "                ans+='1'\n",
    "                return ans\n",
    "            else:\n",
    "                ans+='0'\n",
    "            p-=1\n",
    "        return 'ERROR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        ans=[\"0.\"]\n",
    "        tmp=num\n",
    "        while len(ans) <= 32 and tmp != 0:\n",
    "            tmp *= 2\n",
    "            digit = int(tmp)\n",
    "            ans.append(str(digit)) \n",
    "            tmp -= digit\n",
    "        return \"\".join(ans) if len(ans) <= 32 else \"ERROR\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "        if len(binary) > 32:\n",
    "            return \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = [\"0.\"]\n",
    "        for _ in range(6): \n",
    "            num *= 2\n",
    "            if num < 1:\n",
    "                s.append('0')\n",
    "            else:\n",
    "                s.append('1')\n",
    "                num -= 1\n",
    "                if num == 0:\n",
    "                    return ''.join(s)\n",
    "        return \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = \"0.\"\n",
    "        while num:\n",
    "            num *= 2\n",
    "            if len(res) >= 32:\n",
    "                return \"ERROR\"\n",
    "            if num >= 1:\n",
    "                res += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                res += \"0\"\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        digit = 1\n",
    "        factor = 0.5\n",
    "        err = 1\n",
    "        ans = \"0.\"\n",
    "        while digit<=32 and num>1e-12:\n",
    "            if num >= factor:\n",
    "                num -= factor\n",
    "                ans += \"1\"\n",
    "            else: # num < factor\n",
    "                ans += \"0\"\n",
    "            digit += 1\n",
    "            factor *= 0.5\n",
    "        if digit>32:\n",
    "            return \"ERROR\"\n",
    "        # print(\"digit=\",digit)\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        ans = '0.'\n",
    "        cnt = 0\n",
    "        while num > 0 and cnt < 30:\n",
    "            num = num * 2\n",
    "            if num >= 1:\n",
    "                ans = ans + '1'\n",
    "                num = num - 1\n",
    "            else:\n",
    "                ans = ans + '0'\n",
    "            cnt = cnt + 1\n",
    "        # print(num)\n",
    "        if num > 0:\n",
    "            return 'ERROR'\n",
    "        else:\n",
    "            return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "        if len(binary) > 32:\n",
    "            return \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        # 十进制小数转二进制小数\n",
    "        # 小数部分乘以 2，取整数部分作为二进制小数的下一位\n",
    "        # 取小数部分作为下一次乘法的被乘数继续乘以2，直到小数部分为0或二进制小数的长度超过32\n",
    "        res=\"0.\"\n",
    "        while len(res)<=32 and num!=0:\n",
    "            num*=2\n",
    "            digit=int(num)\n",
    "            res+=str(digit)\n",
    "            num-=digit # 将整数部分置为0\n",
    "        return \"ERROR\" if num else res\n",
    "        # 如果小数部分不为 0，说明该实数无法用 32 位以内的二进制表示，返回字符串 \"ERROR\"，否则返回转换后的二进制小数\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        if num < 0:\n",
    "            return \"ERROR\"\n",
    "        n = int(num)\n",
    "        p1 = bin(n)[2:]\n",
    "        m = num - n\n",
    "        f = 0.5\n",
    "        c = 0\n",
    "        p2 = \"\"\n",
    "        while m > 0 and c < 6:\n",
    "            if m >= f:\n",
    "                p2 += '1'\n",
    "                m -= f\n",
    "            else:\n",
    "                p2 += '0'\n",
    "            f *= 0.5\n",
    "            c += 1\n",
    "        if m != 0:\n",
    "            return \"ERROR\"\n",
    "        return p1+'.'+p2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        b = \"0.\"\n",
    "        while len(b) <= 32 and num != 0:\n",
    "            num *= 2\n",
    "            digit = int(num)\n",
    "            b += str(digit)\n",
    "            num -= digit\n",
    "        if len(b) <= 32:\n",
    "            return b\n",
    "        return \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        ans = ['0','.']\n",
    "        for i in range(1,31):\n",
    "            x = 2 ** (-i)\n",
    "            if num == x:\n",
    "                ans.append('1')\n",
    "                return ''.join(ans)\n",
    "            elif num >= x:\n",
    "                ans.append('1')\n",
    "                num -= x\n",
    "            else:\n",
    "                ans.append('0')\n",
    "        return 'ERROR'\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = '0.'\n",
    "        while num:\n",
    "            print(num)\n",
    "            num *= 2\n",
    "            print('--> ', num)\n",
    "            if num >= 1:\n",
    "                s += '1'\n",
    "                num = float('0' + str(num)[1:])\n",
    "                print('to ', num)\n",
    "            else:\n",
    "                s += '0'\n",
    "            print('------> ', num)\n",
    "            if len(s) > 32: return \"ERROR\"\n",
    "\n",
    "        return s\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = \"0.\"\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                res += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                res += \"0\"\n",
    "        if len(res) > 32:\n",
    "            return \"ERROR\"\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        N=32\n",
    "        if num==0 or num==1:\n",
    "            return str(num)\n",
    "        else:\n",
    "            res=\"0.\"\n",
    "            for i in range(N):\n",
    "                num*=2\n",
    "                if num==1:\n",
    "                    res+=\"1\"\n",
    "                    break\n",
    "                elif num>1:\n",
    "                    res+=\"1\"\n",
    "                    num-=1\n",
    "                else:\n",
    "                    res+=\"0\"\n",
    "        return res if len(res)<=32 else \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = '0.'\n",
    "        while len(res)<=32 and num!=0:\n",
    "            num *= 2\n",
    "            digit = int(num)\n",
    "            res += str(digit)\n",
    "            num -= digit\n",
    "        return res if len(res)<=32 else 'ERROR'\n",
    "\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        buffer = [\"0\", \".\"] + ['0'] * 30\n",
    "        for i in range(2, 32):\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                buffer[i] = '1'\n",
    "                num = num - 1\n",
    "                if num == 0:\n",
    "                    return \"\".join(buffer[:i+1])\n",
    "            else:\n",
    "                buffer[i] = '0'\n",
    "            \n",
    "        if num != 0:\n",
    "            return \"ERROR\"\n",
    "        return \"\".join(buffer)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = '0.'\n",
    "        flag = True\n",
    "        while num!=0:\n",
    "            num*=2\n",
    "            res+=str(math.floor(num))\n",
    "            if len(res)>32:return \"ERROR\"\n",
    "            if num>=1:num-=1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "        if len(binary) > 32:\n",
    "            return \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "        if len(binary) > 32:\n",
    "            return \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        str1 = ['0.']\n",
    "        for i in range(0,6):\n",
    "            num *= 2\n",
    "            if num < 1:\n",
    "                str1.append('0')\n",
    "            else:\n",
    "                str1.append('1')  \n",
    "                num -= 1\n",
    "                if num == 0:\n",
    "                    return ''.join(str1)\n",
    "        return 'ERROR'              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        digits = []\n",
    "        base = 0.5\n",
    "        while num > 0 and len(digits) < 30:\n",
    "            if num >= base:\n",
    "                digits.append(\"1\")\n",
    "                num -= base\n",
    "            else:\n",
    "                digits.append(\"0\")\n",
    "            base /= 2.\n",
    "        \n",
    "        if num == 0:\n",
    "            return '0.'+\"\".join(digits)\n",
    "        return \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s=[\"0.\"]\n",
    "        for _ in range(6):\n",
    "            num*=2\n",
    "            if num<1:\n",
    "                s.append('0')\n",
    "            else:\n",
    "                s.append('1')\n",
    "                num-=1\n",
    "                if num==0:\n",
    "                    return ''.join(s)\n",
    "        return 'ERROR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res=\"0.\"\n",
    "        while len(res)<=32 and num!=0:\n",
    "            num*=2\n",
    "            digit=int(num)\n",
    "            res+=str(digit)\n",
    "            num-=digit\n",
    "        return res if len(res)<=32 else \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        N=50\n",
    "        if num==0 or num==1:\n",
    "            return str(num)\n",
    "        else:\n",
    "            res=\"0.\"\n",
    "            for i in range(N):\n",
    "                num*=2\n",
    "                if num==1:\n",
    "                    res+=\"1\"\n",
    "                    break\n",
    "                elif num>1:\n",
    "                    res+=\"1\"\n",
    "                    num-=1\n",
    "                else:\n",
    "                    res+=\"0\"\n",
    "        return res if len(res)<=32 else \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        # 十进制小数转二进制小数的方法是：小数部分乘以 2，取整数部分作为二进制小数的下一位，小数部分作为下一次乘法的被乘数，直到小数部分为 0 或者二进制小数的长度超过 32 位。由于实数介于 0 和 1 之间，所以其整数部分一定是 0，我们只需要将小数部分，按照上述方法转换为二进制小数即可。如果小数部分不为 0，说明该实数无法用 32 位以内的二进制表示，返回字符串 \"ERROR\"\n",
    "        ans = '0.'\n",
    "        while len(ans) < 32 and num:\n",
    "            num *= 2\n",
    "            x = int(num) # 整数部分\n",
    "            ans += str(x) # 作为此位\n",
    "            num -= x  # 小数部分，继续计算\n",
    "        return 'ERROR' if num else ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = [\"0.\"]\n",
    "        for _ in range(6):  # 至多循环 6 次\n",
    "            num *= 2\n",
    "            if num < 1:\n",
    "                s.append('0')\n",
    "            else:\n",
    "                s.append('1')\n",
    "                num -= 1\n",
    "                if num == 0:\n",
    "                    return ''.join(s)\n",
    "        return \"ERROR\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = [\"0.\"]\n",
    "        for _ in range(6):  # 至多循环 6 次\n",
    "            num *= 2\n",
    "            if num < 1:\n",
    "                s.append('0')\n",
    "            else:\n",
    "                s.append('1')\n",
    "                num -= 1\n",
    "                if num == 0:\n",
    "                    return ''.join(s)\n",
    "        return \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = \"0.\"\n",
    "        while len(res) <= 32 and num != 0:\n",
    "            num *= 2\n",
    "            digit = int(num)\n",
    "            res += str(digit)\n",
    "            num -= digit\n",
    "        return res if len(res) <= 32 else \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "        if len(binary) > 32:\n",
    "            return \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        # visitied = set([num])\n",
    "        res = '0.'\n",
    "        flag = True\n",
    "        while num!=0:\n",
    "            num*=2\n",
    "            res+=str(math.floor(num))\n",
    "            if len(res)>32:return \"ERROR\"\n",
    "            if num>=1:num-=1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        base = 0.5\n",
    "        value = '0.'\n",
    "        while num > 0 and len(value) < 32:\n",
    "            while base > num:\n",
    "                base = base / 2.\n",
    "                value += '0'\n",
    "            value += '1'\n",
    "            num -= base\n",
    "            base /= 2.\n",
    "        \n",
    "        if num == 0:\n",
    "            return value\n",
    "        return 'ERROR'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "        if len(binary) > 32:\n",
    "            return \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        while num > 0:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "        if len(binary) > 32:\n",
    "            return \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = \"0.\"\n",
    "        while num != 0:\n",
    "            num, integer = math.modf(num*2)\n",
    "            s += str(int(integer))\n",
    "            if len(s) > 32:\n",
    "                return \"ERROR\"\n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = \"0.\"\n",
    "        for i in range(32):\n",
    "            num *= 2\n",
    "            if num == 1:\n",
    "                return res + '1'\n",
    "            if num > 1:\n",
    "                res += '1'\n",
    "                num -= 1\n",
    "            else:\n",
    "                res += '0'\n",
    "                continue\n",
    "        return \"ERROR\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = [\"0.\"]\n",
    "        for _ in range(6):\n",
    "            num *= 2\n",
    "            if num < 1 : \n",
    "                s.append('0')\n",
    "            else:\n",
    "                s.append('1')\n",
    "                num -= 1\n",
    "                if num == 0:\n",
    "                    return ''.join(s)\n",
    "        return 'ERROR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        a = '0.'\n",
    "        while len(a) <= 32 and num != 0:\n",
    "            num *= 2\n",
    "            a += str(int(num))\n",
    "            num -= int(num)\n",
    "        return a if len(a) <= 32 else 'ERROR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = \"0.\"\n",
    "        while num != 0:\n",
    "            num, integer = math.modf(num*2)\n",
    "            s += str(int(integer))\n",
    "            if len(s) > 32:\n",
    "                return \"ERROR\"\n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res, count = \"0.\", 0\n",
    "        \n",
    "        while num > 0 and count < 32:\n",
    "            num = 2 * num\n",
    "            add = int(num)\n",
    "            res += str(add)\n",
    "            num -= add\n",
    "            count += 1\n",
    "        return res if num==0 else \"ERROR\"\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = []\n",
    "        i = 0\n",
    "        while num != 0 and i < 30:\n",
    "            num = num * 2\n",
    "            front = num // 1\n",
    "            num = num - front\n",
    "            res.append(str(int(front)))\n",
    "            i += 1\n",
    "        ans = '0.' + ''.join(res)\n",
    "        # for i in res:\n",
    "        #     ans += str(i)\n",
    "        return ans if i < 30 else 'ERROR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        i = 32\n",
    "        res = '0.'\n",
    "        while num != 0:\n",
    "            if i < 0:\n",
    "                break \n",
    "            num *= 2\n",
    "            res += str(num).split('.')[0]\n",
    "            num = float('0.'+str(num).split('.')[1])\n",
    "            i -= 1\n",
    "\n",
    "        if i < 0:\n",
    "            return 'ERROR'\n",
    "        else:\n",
    "            return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        s = '0.'\n",
    "        while num:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                s += '1'\n",
    "                num = float('0' + str(num)[1:])\n",
    "            else:\n",
    "                s += '0'\n",
    "\n",
    "            if len(s) > 32: return \"ERROR\"\n",
    "\n",
    "        return s\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = '0.'\n",
    "        i = 0\n",
    "        while num != 0 and len(res) < 32:\n",
    "            num = num * 2\n",
    "            front = num // 1\n",
    "            num = num - front\n",
    "            res += (str(int(front)))\n",
    "\n",
    "        #ans = '0.' + ''.join(res)\n",
    "        # for i in res:\n",
    "        #     ans += str(i)\n",
    "        #return ans if i < 30 else 'ERROR'\n",
    "        return res if len(res) < 32 else 'ERROR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        ans = '0.'\n",
    "        while len(ans) < 32 and num:\n",
    "            num *= 2\n",
    "            x = int(num)\n",
    "            ans += str(x)\n",
    "            num -= x\n",
    "        return 'ERROR' if num else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        binary = \"0.\"\n",
    "        count = 2\n",
    "        while num > 0 and count < 33:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                binary += \"1\"\n",
    "                count += 1\n",
    "                num -= 1\n",
    "            else:\n",
    "                binary += \"0\"\n",
    "                count += 1\n",
    "        if count == 33:\n",
    "            binary = \"ERROR\"\n",
    "        return binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "\n",
    "        res = '0.'\n",
    "\n",
    "        while len(res) < 32:\n",
    "            num *= 2\n",
    "            if num >= 1:\n",
    "                res += '1'\n",
    "                num -= 1\n",
    "                if num == 0.0:\n",
    "                    return res\n",
    "\n",
    "            else:\n",
    "                res += '0'\n",
    "        \n",
    "        return 'ERROR'\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printBin(self, num: float) -> str:\n",
    "        res = '0.'\n",
    "\n",
    "        while len(res) <= 32 and num != 0:\n",
    "            num *= 2\n",
    "            digit = int(num)\n",
    "            res += str(digit)\n",
    "            num -= digit\n",
    "        return res if len(res) <= 32 else \"ERROR\""
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
